public function get_post_by_post_name($post_name, $post_type = '', $post_status = 'publish')
{
global $wpdb;
$sql = array();
$sql[] = "SELECT p.*";
$sql[] = "FROM $wpdb->posts AS p";
$sql[] = "WHERE p.post_name " . $this->__get_sql_for_in($post_name);
if (!empty($post_type))
$sql[] = "AND p.post_type = '$post_type'";
if(!empty($post_status))
$sql[] = "AND p.post_status = '$post_status'";
$result = $wpdb->get_results(implode(' ', $sql));
if (empty($result))
return null;
return is_array($post_name) ? $result : $result[0];
}
protected function __get_sql_for_in($values, $is_number = false)
{
$quote = $is_number ? "" : "'";
$is_array = is_array($values);
$not = ($is_array && is_string($values[0]) && preg_match('/^.*\!=.+$/',$values[0])) || (!$is_array && is_string($values) && preg_match('/^.*\!=.+$/',$values));
$in = $not ? ' NOT IN' : ' IN';
$eq = $not ? '!=' : '=';
if($is_array){
foreach ($values as $i => $value) {
$values[$i] = preg_replace('/^(\!=|=)/', '', esc_sql($value));
$values[$i] = preg_replace('/^ /', '', esc_sql($value));
}
}else{
$values = preg_replace('/^(\!=|=)/','',esc_sql($values));
$values = preg_replace('/^ /','',esc_sql($values));
}
return $is_array && 1 < count($values) ? "{$in} ({$quote}" . implode("{$quote},{$quote}", $values) . "{$quote})" : ($is_array ? "{$eq} {$quote}{$values[0]}{$quote}" : "{$eq} {$quote}$values{$quote}");
}